# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        '''
            反转链表2：
                先看看能否反转前N个节点，即用一个节点先记录第N+1个节点然后链接起来
                再看看这个问题，如果left=1，则就跟反转前N个节点是一样的。
        '''
        if left == 1:
            return self.reverseN(head,right)
                    
        head.next = self.reverseBetween(head.next,left-1,right-1)

        return head

        #反转前N个节点
    def reverseN(self,head,n):
        sufix = None
        if n == 1:
            sufix = head.next
            return head
        last = self.reverseN(head.next,n -1)
        head.next.next = head
        head.next = sufix
        return last
    